package com.wyx.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wyx.Common.R;
import com.wyx.Pojo.AddressBook;
import com.wyx.Pojo.User;
import com.wyx.Service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 地址信息模块
 */
@RestController
@RequestMapping("/addressBook")
public class AddressController {
    @Autowired
    private AddressBookService addressBookService;


    /**
     * 获取地址信息
     *
     * @param request
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> getlist(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        Long id = user.getId();
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId, id);
        List<AddressBook> list = addressBookService.list(wrapper);
        return R.success(list);
//        AddressBook addressBook=
    }

    /**
     * 新增地址功能
     *
     * @param addressBook
     * @param request
     * @return
     */
    @PostMapping
    public R<String> addnewAddress(@RequestBody AddressBook addressBook, HttpServletRequest request) {

        User user = (User) request.getSession().getAttribute("user");
        //得到登录用户id然后根据id存到AddressBook数据库里面
        Long id = user.getId();
        addressBook.setUserId(id);
        addressBookService.save(addressBook);
        return R.success("新增成功");
    }

    /**
     * 显示默认地址
     * @param request
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> get_default(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        Long id = user.getId();
        LambdaQueryWrapper<AddressBook>wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId,id).eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(wrapper);
        if (one==null){
            AddressBook two=new AddressBook();
            two.setPhone("请输入你的手机号");
            two.setConsignee("请输入收货人姓名");
            two.setDetail("请输入地址");
            return R.success(two);
        }
        return R.success(one);
    }

    /**
     * 设置为默认地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<AddressBook> is_default(@RequestBody AddressBook addressBook, HttpServletRequest request) {
        // LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        //        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        //        wrapper.set(AddressBook::getIsDefault, 0);
        //        //SQL:update address_book set is_default = 0 where user_id = ?
        //        addressBookService.update(wrapper);
        //
        //        addressBook.setIsDefault(1);
        //        //SQL:update address_book set is_default = 1 where id = ?
        //        addressBookService.updateById(addressBook);
        //        return R.success(addressBook);
        User user = (User) request.getSession().getAttribute("user");
//        Long id = addressBook.getId();
//        AddressBook byId = addressBookService.getById(id);
        //将当前用户的其他地址的IsDefault设置为0
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId, user.getId());
        List<AddressBook> list = addressBookService.list(wrapper);
        for (AddressBook book : list) {
            book.setIsDefault(0);
        }
        addressBookService.updateBatchById(list);
        //将IsDefault设置为1
        addressBook.setIsDefault(1);
        //修改数据
        addressBookService.updateById(addressBook);
        return R.success(addressBook);

    }

    /**
     * 回显数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> GetAddress(@PathVariable Long id) {
        if (id == null) {
            return R.error("查无此数据，请稍后再试");
        } else {
            AddressBook addressBook = addressBookService.getById(id);
            return R.success(addressBook);
        }
    }

    /**
     * 修改地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook) {
        if (addressBook == null) {
            return R.error("地址传入错误，请重新输入");
        }
        Long id = addressBook.getId();
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getId, id);
        addressBookService.update(addressBook, wrapper);
        return R.success("修改成功~");
    }

    /**
     * 删除某一个地址
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> deleteaddressBook(Long id) {
        if (id == null) {
            return R.error("数据有误，请稍后再试");
        }
        AddressBook byId = addressBookService.getById(id);
        if (byId != null) {
            addressBookService.removeById(id);
            return R.success("删除成功");
        } else {
            return R.error("地址不存在，请刷新后再试");
        }

    }
}
